package com.nhatth.contactsharer;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class ComfirmActivity extends Activity {
	
	String sms = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.confirm_layout);
		super.onCreate(savedInstanceState);
		long[] ids = getIntent().getLongArrayExtra("contacts_id");
		String where = "";
		for (int i = 0; i < ids.length; i++) {
			Log.d("ComfirmActivity", "id["+i+"]: "+ids[i]);
			if (i == ids.length - 1) {
				where += Contacts._ID + "=" + ids[i];
			} else {
				where += Contacts._ID + "=" + ids[i] + " or ";
			}
		}
		Cursor c = getContentResolver().query(Contacts.CONTENT_URI,
				new String[] { Contacts._ID, Contacts.DISPLAY_NAME }, where,
				null, null);
		startManagingCursor(c);
		String phoneNumber = "";
		while (c.moveToNext()) {
			String current = "";
			String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
			Cursor phones = getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "+ contactId, null, null);
			
			phones.moveToFirst();
			phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
			phones.close();
			current += c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME))+ " "+phoneNumber + " ";
			sms += current + "\n";
		}
		sms = sms.substring(0, sms.length() - 2);
		TextView txtSms = (TextView)findViewById(R.id.confirmLayout_txtSms);
		txtSms.setText(sms);
		Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.putExtra("sms_body", sms); 
        sendIntent.setType("vnd.android-dir/mms-sms");
        startActivity(sendIntent);
	}
	
	public void btnSend_OnClick(View v){
		Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.putExtra("sms_body", sms); 
        sendIntent.setType("vnd.android-dir/mms-sms");
        startActivity(sendIntent);
	}

}
